import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {AppGlobal, AppService} from "../../app/app.service";

/**
 * Generated class for the PubalterPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
/*公共修改页面,用法:this.navCtrl.push(this.pubalterPage,{alterparams:this.user.nickname,title:'修改昵称',note:'帅气的昵称提升人气',type:'1',......});
* type:1-用户昵称
*
*
* */
@IonicPage()
@Component({
  selector: 'page-pubalter',
  templateUrl: 'pubalter.html',
})
export class PubalterPage {
  title:any;
  alterparams:any;
  note:any;
  user:any;
  type:any;
  showinput:boolean = false;
  showselect:boolean = false;

  constructor(public navCtrl: NavController, public navParams: NavParams,public appService: AppService) {
    this.alterparams = this.navParams.get("alterparams");
    this.title = this.navParams.get("title");
    this.note = this.navParams.get("note");
    this.type = this.navParams.get("type");
    if(this.type == '1'){
      this.showinput = true;
    }else if(this.type == '2'){
      this.showselect = true;
    }
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PubalterPage');
  }
  //保存修改
  savealter(){
    if(this.type == '1'){
      let user_id = this.navParams.get("user_id");
      let sessiontoken = this.navParams.get("sessiontoken");
      console.log(this.alterparams);
      this.appService.httpPost(AppGlobal.API.updateUser, {user_id:user_id,nickname:this.alterparams,sessiontoken:sessiontoken}, rs => {
        console.debug(rs);
        this.user = rs.data;
        let succ = rs.state;
        console.debug(succ);
        if (succ){
          console.debug(succ);
            localStorage.setItem('user',JSON.stringify(this.user));//保存用户信息
        }
        this.navCtrl.pop();
      });
    }else if (this.type == '2'){
      let user_id = this.navParams.get("user_id");
      console.log(this.alterparams);
      this.appService.httpPost(AppGlobal.API.updateUser, {user_id:user_id,companyname:this.alterparams}, rs => {
        console.debug(rs);
        this.user = rs.data;
        let succ = rs.state;
        console.debug(succ);
        if (succ){
          console.debug(succ);
          localStorage.setItem('user',JSON.stringify(this.user));//保存用户信息
        }
        this.navCtrl.pop();
      });
    }
  }
}
